/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.history;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kommando.core.catalog.CatalogObject;

/**
 * 
 * @author Peter De Bruycker
 */
public class History implements Serializable {
    private static final long serialVersionUID = 1L;

    private Map<String, List<String>> history = new HashMap<String, List<String>>();

    public void addObject(CatalogObject object, String input) {
        if (!history.containsKey(input)) {
            history.put(input, new ArrayList<String>());
        }

        history.get(input).add(object.getId());
    }

    public float getRankModifier(CatalogObject object, String input) {
        List<String> historyForInput = history.get(input);

        if (historyForInput == null || !(historyForInput.contains(object.getId()))) {
            return 0;
        }

        float score = historyForInput.size();

        for (int i = 0; i < historyForInput.size(); i++) {
            String objectId = historyForInput.get(i);
            if (!objectId.equals(object.getId())) {
                score -= (float) (i + 1) / historyForInput.size();
            }
        }

        return score / historyForInput.size();
    }
}
